package cn.springBoot.controller;

import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.identity.Group;
import org.camunda.bpm.engine.identity.GroupQuery;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.identity.UserQuery;
import org.camunda.bpm.engine.impl.persistence.entity.GroupEntity;
import org.camunda.bpm.engine.impl.persistence.entity.TenantEntity;
import org.camunda.bpm.engine.impl.persistence.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ljx
 * @description:  用户服务
 * @class cn.springBoot.controller.IdentityController
 * @create date: 2021/9/16
 *
 *
 * Identity service 是在各种租户、组、用户上的抽象，这其中由以下几个实体组成：
 *
 * User: 一个由唯一ID确定的用户；
 * Group: 一个由唯一ID确定的组；
 * Membership: 用户与组的关系；
 * Tenant: 一个由唯一ID确定的租户；
 * Tenant Membership: 租户与组/用户的关系；
 *
 *
 * Camunda BPM 分为只读与可写用户实现，只读用户提供了只读的底层连接到用户与组数据库，可写用户则提供了对用户的增删改查。
 *
 * 为了提供定制化的身份实现，可以实现以下两个接口：
 *
 * org.camunda.bpm.engine.impl.identity.ReadOnlyIdentityProvider
 * org.camunda.bpm.engine.impl.identity.WritableIdentityProvider
 *
 */
@RequestMapping("/identity")
@RestController
public class IdentityController {

    //身份验证
    @Autowired
    IdentityService identityService;


/**
 * *************************************** <<<用户>>>>******************************************************************
 */


   /**
    * @description:  创建用户  由camunda自动为数据库加密加盐
    * @param:id
    * @Param: firstname
    * @Param: lastname
    * @Param: email
    * @Param: pwd
    * @author: ljx
    * @create date: 2021/9/16
    * @version: V1.0
    * @return: java.lang.String
    */

    public String addUser(String id, String firstname, String lastname, String email, String pwd) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        userEntity.setFirstName(firstname);
        userEntity.setLastName(lastname);
        userEntity.setEmail(email);
        userEntity.setPassword(pwd);
        identityService.saveUser(userEntity);
        return id;
    }

     /** 
      * @description: 根据id 获取用户 
      * @param:id 
      * @author: ljx
      * @create date: 2021/9/16 
      * @version: V1.0 
      * @return: org.camunda.bpm.engine.identity.User 
      */

    public User getUserById(String id) {
        User user = identityService.createUserQuery().userId(id).singleResult();
        return user;
    }

     /**
      * @description: 获取用户列表
      * @param:firstResult
      * @Param: maxResult
      * @author: ljx
      * @create date: 2021/9/16
      * @version: V1.0
      * @return: java.util.List<org.camunda.bpm.engine.identity.User>
      */

    public List<User> getUsers(int firstResult, int maxResult) {


        UserQuery userQuery = identityService.createUserQuery();
        /*  userQuery.userIdIn("peng1", "peng2")
                .userEmailLike("%qq%")
                .list();*/



        return identityService.createUserQuery().listPage(firstResult, maxResult);
    }


    /**
     * @description:  根据用户id删除用户
     * @param:userId
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: void
     */

    public void delUser(String userId) {
        identityService.deleteUser(userId);
    }



     /**
      * @description:  获取租户下的用户列表
      * @param:tenantId
      * @Param: first
      * @Param: max
      * @author: ljx
      * @create date: 2021/9/16
      * @version: V1.0
      * @return: java.util.List<org.camunda.bpm.engine.identity.User>
      */

    public List<User> getUsersFromTenant(String tenantId, int first, int max) {
        return identityService.createUserQuery().memberOfTenant(tenantId).listPage(first, max);
    }

/**
 * *************************************** <<<组>>>>******************************************************************
 */


    /**
     * @description:  新增组
     * @param:id
     * @Param: name
     * @Param: rev
     * @Param: type
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: void
     */

    public void addGroup(String id, String name, int rev, String type) {
        GroupEntity groupEntity = new GroupEntity();
        groupEntity.setId(id);
        groupEntity.setName(name);
        groupEntity.setRevision(rev);
        groupEntity.setType(type);
        identityService.saveGroup(groupEntity);
    }


    /**
     * @description: 将用户添加到组
     * @param:userId
     * @Param: groupId
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: void
     */

    public void createMembership(String userId, String groupId) {
        identityService.createMembership(userId, groupId);
    }

    /**
     * @description:  根据分页获取组信息
     * @param:first
     * @Param: max
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: java.util.List<org.camunda.bpm.engine.identity.Group>
     */

    public List<Group> getGroups(int first, int max) {
        GroupQuery groupQuery = identityService.createGroupQuery();
        return groupQuery.listPage(first, max);
    }


     /**
      * @description:   * 根据组ID删除组  * 可先获取组列表，再根据获取的ID删除
      * @param:groupId
      * @author: ljx
      * @create date: 2021/9/16
      * @version: V1.0
      * @return: void
      */

    public void delGroup(String groupId) {
        identityService.deleteGroup(groupId);
    }


    /**
     * @description:  将用户从组中删除
     * @param:userId
     * @Param: groupId
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: void
     */

    public void delMembership(String userId, String groupId) {
        identityService.deleteMembership(userId, groupId);
    }



     /**
      * @description:  获取租户下的组
      * @param:tenantId
      * @Param: first
      * @Param: max
      * @author: ljx
      * @create date: 2021/9/16
      * @version: V1.0
      * @return: java.util.List<org.camunda.bpm.engine.identity.Group>
      */

    public List<Group> getGroupsFromTenant(String tenantId, int first, int max) {
        return identityService.createGroupQuery().memberOfTenant(tenantId).listPage(first, max);
    }

/**
 * *************************************** <<<租户>>>>******************************************************************
 */

    /**
     * @description:  创建一个租户
     * @param:id
     * @Param: name
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: void
     */

    public void saveTenant(String id, String name) {
        TenantEntity tenantEntity = new TenantEntity();
        tenantEntity.setId(id);
        tenantEntity.setName(name);
        identityService.saveTenant(tenantEntity);
    }
    /**
     * @description:  将用户添加到租户下
     * @param:tenantId
     * @Param: userId
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: void
     */

    public void saveTenantUserMembership(String tenantId, String userId) {
        identityService.createTenantUserMembership(tenantId, userId);
    }

    /**
     * @description:  将组添加到租户下
     * @param:tenantId
     * @Param: groupId
     * @author: ljx
     * @create date: 2021/9/16
     * @version: V1.0
     * @return: void
     */

    public void saveTenantGroupMembership(String tenantId, String groupId) {
        identityService.createTenantUserMembership(tenantId, groupId);
    }

}
